1712C - Sort Zero - CodeForces Solution


greedy sortings two pointers

Please click on ads to support us..

Python Code:

def solve():
    n = int(input())
    a = [int(x) for x in input().split()]
    if a == sorted(a):
        print(0)
    else:
        s = set()
        index = -1
        for i in range(n - 2, -1, -1):
            if a[i] > a[i + 1]:
                index = i
                break
        for i in range(0, index + 1):
            s.add(a[i])
        index2 = -1
        for i in range(n - 1, index, -1):
            if a[i] in s:
                index2 = i
                break

        for i in range(index + 1, index2):
            s.add(a[i])

        print(len(s))
    return None


t = int(input())
for _ in range(t):
    solve()

C++ Code:

#include<bits/stdc++.h>
using namespace std;

#define fastIO  ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define ll long long
#define max(a,b) (((b)>(a)) ? b : a )
#define min(a,b) (((a)<(b)) ? a : b)
#define endl "\n"
#define space " "

long long binarySearch(long long key, long long mouse[], int s, int e)
{
    while(s<=e)
    {
        int mid=(s+e)/2;

        if (mouse[mid]==key)          
            return mid;
        else if (mouse[mid]>key)      
            e=mid-1;
        else                        
            s=mid+1;
    }
    return -1;

    //O(logn), O(1)
}

long long gcd(long long a, long long b)
{
    return b == 0 ? a : gcd(b, a % b);
    //O(log(min(a,b)), O(log(min(a,b))
}

long long LCM(int a, int b)
{
    return a*b/gcd(a,b);
}

//---------------------------------------------------------------------------------------------------//

void solve()
{
    int n;
    cin>>n;

    int arr[n];
    map<int, vector<int>> mp;
    for(int i=0;i<n;i++)
    {
        cin>>arr[i];
        mp[arr[i]].push_back(i);
    }

    int maxx=-1;

    for(int i=n-1;i>=1;i--)
    {
        if (arr[i-1]>arr[i])
        {
            
            maxx=mp[arr[i-1]][mp[arr[i-1]].size()-1];
            break;
        }
    }
    if (maxx==-1)
    {
        cout<<0<<endl;
        return;
    }
    int i=maxx;
    for(;i>=0;i--)
    {
        maxx=max(mp[arr[i]][mp[arr[i]].size()-1],
                    maxx    );
    }
    
    set<int> sett;
    for(int i=0;i<=maxx;i++)
    {
        sett.insert(arr[i]);
    }
    cout<<sett.size()<<endl;
}

int main()
{
    fastIO;
    int t;
    cin>>t;
    while(t--)
    {
        solve();


    }
}


Comments

Submit
0 Comments
More Questions

448A - Rewards
1622A - Construct a Rectangle
1620A - Equal or Not Equal
1517A - Sum of 2050
620A - Professor GukiZ's Robot
1342A - Road To Zero
1520A - Do Not Be Distracted
352A - Jeff and Digits
1327A - Sum of Odd Integers
1276A - As Simple as One and Two
812C - Sagheer and Nubian Market
272A - Dima and Friends
1352C - K-th Not Divisible by n
545C - Woodcutters
1528B - Kavi on Pairing Duty
339B - Xenia and Ringroad
189A - Cut Ribbon
1182A - Filling Shapes
82A - Double Cola
45A - Codecraft III
1242A - Tile Painting
1663E - Are You Safe
1663D - Is it rated - 3
1311A - Add Odd or Subtract Even
977F - Consecutive Subsequence
939A - Love Triangle
755A - PolandBall and Hypothesis
760B - Frodo and pillows
1006A - Adjacent Replacements
1195C - Basketball Exercise